<?php

namespace app\admin\model;

use app\common\model\TimeModel;
use think\Exception;
use think\Model;

class ShopGoods extends TimeModel
{

    protected $name = "shop_goods";
    protected $deleteTime = 'delete_time';

    public function cate()
    {
        return $this->belongsTo('app\admin\model\ShopCate', 'cate_id', 'id');
    }

    public function store()
    {
        return $this->belongsTo('app\admin\model\ShopStore', 'store_id', 'id');
    }



    public static function onBeforeWrite(Model $model)
    {
        //自动根据店铺获取分类
        try {
            $model->cate_id = ShopStore::where('id', $model->store_id)->value('cate_id');
        } catch (Exception $e) {
            throw new Exception('无法获取店铺分类');
        }
        if (isset($model->brief)) {
//            $model->brief = base64_encode($model->brief);
        }
    }

    public static function onAfterRead(Model $model)
    {
        //解密编码
        if (isset($model->brief)) {
//            $model->brief = base64_decode($model->brief);
        }
    }

}